import pao = require('../../../public');
import paoClient = require('../../client');
import paoControls = require('../../controls');
import mainForm = require('../navbarMainForm');

/**
 * 网页应用主视图
 */
@pao.addon("网页应用通用主视图","网页应用通用主视图,用于PC端通用网页布局的主视图","pao.client")
@paoClient.template("pao/client/mainForm/common/commonMainForm.html", 
                    "pao/client/mainForm/common/commonMainForm.css")
export class commonMainForm extends mainForm.navbarMainForm { 
    
    /**
     * 网页标题
     */
    title?:string;

    /**
     * 网页Brand
     */
    $brand?:JQuery = undefined;

    /**
     * 辅助导航条
     */
    $sidebar?:JQuery = undefined;

    /**
     * 漂浮工具条
     */
    $floatTools?:JQuery = undefined;

    /**
     * 工具条
     */
    $tools?:JQuery = undefined;

    /**
     * 更改页面
     * @param page 页面
     */
    changePage? (page: paoControls.tabPage) {
        let view : paoClient.baseView = pao.getObject(page.view);
        this.$title.html(page.title);

        this.navigate(view);
    }

    /**
     * 绑定事件
     */
    onAfterBind? () {
        this.$brand.html(this.title);
        // 渲染导航页面
        if(this.navPages) {
            for(let nav of this.navPages) {
                nav.onChangeView = (page: paoControls.tabPage) =>{
                    this.changePage(page);
                };
                nav.appendTo(this.$navBar);
                nav.appendTo(this.$sidebar);
            }
        }

        // 加载首页
        if (this.navPages && this.navPages[0]) {
            this.changePage(this.navPages[0]);
        }
    }
        
    /**
     * 导航到视图，如果视图是string类型，表示url，如果是baseView，表示直接导航到对象
     */
    navigate?(view: string | paoClient.baseView, caption?: string) : void{
        let targetView : paoClient.baseView = this.getView(view, caption);
        if(!targetView) 
            throw new Error(`加载视图${view}失败`);
          
        this.$content.children().hide();
        if(targetView.element) {
            $(targetView.element).show();
        } else {
            targetView.appendTo(this.$content[0]);
        }
        if(targetView.onShowed){
            targetView.onShowed();
        }
    }

}
